package ar.edu.utn.frre.sistop.simumem.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import ar.edu.utn.frre.sistop.simumem.DataLoadActivity;
import ar.edu.utn.frre.sistop.simumem.R;
import ar.edu.utn.frre.sistop.simumem.common.Preferences;

public class FixedPartitionParameterLoadFragment extends Fragment {

	private Preferences mPreferences;
	Spinner spAlgoritmo;

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getActivity().setTitle(R.string.titleDataLoad_Particiones_Fijas_Parametros);
		// Spinner selector de algoritmos
		spAlgoritmo = (Spinner) getActivity().findViewById(
				R.id.spDataLoadFixedPartition_Algorithm);
		ArrayAdapter<CharSequence> algorithmAdapter = ArrayAdapter.createFromResource(
				getActivity(), R.array.algorithm, android.R.layout.simple_spinner_item);
		algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spAlgoritmo.setAdapter(algorithmAdapter);
		// Recordar el ultimo algoritmo usado
		mPreferences = ((DataLoadActivity) getActivity()).getPreferences();
		spAlgoritmo.setSelection(mPreferences.getAlgoritmoAsignacion());

		// Guardar en preferencias el algoritmo elegido
		spAlgoritmo.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parentView, View view, int position,
					long itemId) {
				mPreferences.setAlgoritmoAsignacion(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parentView) {
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
			@Nullable Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_data_load_fixed_partition_parameter,
				container, false);
		return rootView;
	}

	public int getAlgoritmoIndex() {
		return spAlgoritmo.getSelectedItemPosition();
	}
}
